package com.example.gittest

import android.os.Bundle
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.common.Player.Listener
import androidx.media3.exoplayer.ExoPlayer
import com.example.gittest.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var player: ExoPlayer
    private var isPlay = false
    private var isFastForward = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        player = ExoPlayer.Builder(this).build()
        player.setVideoSurfaceView(binding.videoSv)
        // Build the media item.
        val mediaItem =
            MediaItem.fromUri("https://vd2.bdstatic.com/mda-rcfbd7aqcehj7x1v/cae_h264/1742112354027063414/mda-rcfbd7aqcehj7x1v.mp4?abtest=peav_l52&appver=&auth_key=1742131240-0-0-bfd2388da8b5e1365df6331efb668356&bcevod_channel=searchbox_feed&cd=0&cr=0&did=cfcd208495d565ef66e7dff9f98764da&logid=3039741992&model=&osver=&pd=1&pt=4&sl=496&sle=1&split=513180&vid=16141716737193791869&vt=1")
        // Set the media item to be played.
        player.setMediaItem(mediaItem)
        player.prepare()
        binding.progressBar.max = player.duration.div(1000).toInt()
        val videoWidth = player.videoSize.width
        val videoHeight = player.videoSize.height
        player.addListener(object : Listener {
            override fun onPlaybackStateChanged(playbackState: Int) {
                super.onPlaybackStateChanged(playbackState)
            }

            override fun onPositionDiscontinuity(
                oldPosition: Player.PositionInfo,
                newPosition: Player.PositionInfo,
                reason: Int
            ) {
                super.onPositionDiscontinuity(oldPosition, newPosition, reason)
                if (!isFastForward) {
                    val positionSecond = newPosition.positionMs.div(1000).toInt()
                    binding.progressBar.progress = positionSecond
                    binding.timeTv.text = positionSecond.toString()
                }
            }
        })

        /*横竖屏切换*/
        binding.hvBtn.setOnClickListener {

        }
        //竖屏放大
        binding.vbigBtn.setOnClickListener {

        }

        binding.playAndPauseBtn.setOnClickListener {
            if (!isPlay) {
                player.play()
            } else {
                player.pause()
            }
        }
        binding.progressBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                binding.timeTv.text = progress.toString()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                isFastForward = true
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                val positionMs: Long = (binding.progressBar.progress * 1000).toLong()
                player.seekTo(positionMs)
                isFastForward = false
            }
        })
    }

    override fun onResume() {
        super.onResume()
        player.play()
    }

    override fun onPause() {
        super.onPause()
        player.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        player.release()
    }
}